package com.qmo.edu.moudle.receiver;

import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


import com.qmo.edu.R;
import com.qmo.edu.moudle.activity.MainActivity;
import com.qmo.edu.utils.NotificationsUtils;
import com.qmo.edu.utils.SpUtil;


import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;


/**
 * Created by Administrator on 2017/7/6.
 */

public class MyReceiver extends BroadcastReceiver {
    private String TAG = "MyReceiver";


    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationsUtils.initNotify(context, "", "", "");
        Bundle bundle = intent.getExtras();
        Log.e(TAG, "onReceive - " + intent.getAction());
        SpUtil.setBoolean(context, "isImageRed", true);


//        // 注册 String 类型事件
//        RxBus.getDefault().subscribe(this, new RxBus.Callback<String>() {
//            @Override
//            public void onEvent(String s) {
//                Log.e("eventTag", s);
//            }
//        });
//
//        // 注册带 tag 为 "my tag" 的 String 类型事件
//        RxBus.getDefault().subscribe(this, "my tag", new RxBus.Callback<String>() {
//            @Override
//            public void onEvent(String s) {
//                Log.e("eventTag", s);
//            }
//        });


        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//            receivingNotification(context, bundle);

            Boolean isCheck = SpUtil.getBoolean(context, "isCheckMessage", false);
            if (isCheck) {
                // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
                NotificationsUtils.showIntentActivityNotify(context, "nantong",
                        bundle.getString(JPushInterface.EXTRA_MESSAGE), "", MainActivity.class);

            }
            // 注册广播
            sendBroadcast(context);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "收到了通知");

            Boolean isCheck = SpUtil.getBoolean(context, "isCheckMessage", false);
            if (isCheck) {

                // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
                NotificationsUtils.showIntentActivityNotify(context, "集中供热",
                        bundle.getString(JPushInterface.EXTRA_MESSAGE), "", MainActivity.class);
            }
            // 注册广播
            sendBroadcast(context);

            SpUtil.setBoolean(context,"isNotify",true);
            // 在这里可以做些统计，或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.e(TAG, "用户点击打开了通知");
            // 在这里可以自己写代码去定义用户点击后的行为

            if (SpUtil.getBoolean(context, "isAdmin", false)) {


            } else {
//                Intent intentUser = new Intent(context, HistoryDataActivity.class);  //自定义打开的界面
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("data",1);
//                context.startActivity(intentUser);

            }
            SpUtil.setBoolean(context,"isNotify",false);

        } else {
            Log.e(TAG, "Unhandled intent - " + intent.getAction());
        }

    }

    private void setSoundAndVibrate(Context context, boolean isOpenSound, boolean isOpenVibrate){
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(context);
        builder.statusBarDrawable = R.mipmap.ic_launcher_new;
        //设置推送的图标
        if (isOpenVibrate && !isOpenSound) {
            //只有振动
            builder.notificationDefaults = Notification.DEFAULT_VIBRATE;
        } else if (isOpenSound && !isOpenVibrate) {
            //只有声音
            builder.notificationDefaults = Notification.DEFAULT_SOUND;
        } else if (isOpenSound && isOpenVibrate) {
            //两个都有
            builder.notificationDefaults = Notification.DEFAULT_ALL;
        } else {//只有呼吸灯
            builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
        }
        JPushInterface.setDefaultPushNotificationBuilder(builder);
    }
    /**
     * 发送广播
     */
    private void sendBroadcast(Context context) {
        Intent intent2 = new Intent("com.ztzz.nantong");
        context.sendBroadcast(intent2);
    }

}
